﻿using ITSM.Filter;
using ITSM.Model.Model;
using ITSM.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ITSM.Controllers.User
{
    [Route("api/[controller]")]
    [ApiController]
    public class InspectController : ControllerBase
    {
        private readonly IUserInfo _userinfo;
        private readonly ITokenHelper _tokenHelper;
        public InspectController(IUserInfo userInfo,ITokenHelper tokenHelper)
        {
            _userinfo = userInfo;
            _tokenHelper = tokenHelper;
        }
        /// <summary>
        /// 登录检查，检查是否选择公司、是否填写过工号
        /// </summary>
        /// <param name="RYD_WxNo"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Inspected(string RYD_WxNo)
        {
            if (string.IsNullOrWhiteSpace(RYD_WxNo) || RYD_WxNo == "null")
            {
                return NotFound();
            }
            //根据微信Id获取用户Id
            string RYD_Id = _userinfo.QueryUserId(RYD_WxNo);
            if (RYD_Id == "")
            {
                return NotFound();
            }
            //根据用户id获取用户数据
            var userInfoModel = _userinfo.QueryUserInfo(RYD_Id);
            userInfoModel.RYD_Id = userInfoModel.RYD_Id;
            //判断是否选择公司、是否填写工号
            if (userInfoModel.RYD_Company == null)
            {
                //_tokenHelper.CreateToken(userInfoModel.RYD_Id).TokenStr 
                return Ok(new string[3] { "RYD_Company", userInfoModel.RYD_Id, _tokenHelper.CreateToken(userInfoModel.RYD_Id).TokenStr });
            }
            else if (userInfoModel.RYD_RyderNo == null)
            {
                return Ok(new string[3] { "RYD_RyderNo", userInfoModel.RYD_Id, _tokenHelper.CreateToken(userInfoModel.RYD_Id).TokenStr });
            }
            return Ok(new string[3] { "Ok", userInfoModel.RYD_Id, _tokenHelper.CreateToken(userInfoModel.RYD_Id).TokenStr });
        }

        /// <summary>
        /// 检查工号和身份证后六位是否匹配
        /// </summary>
        /// <param name="inspectRydNo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> InspectRydNo(RYD_InspectRydNo inspectRydNo)
        {
            if (string.IsNullOrWhiteSpace(inspectRydNo.RYD_RyderNo))
            {
                return NotFound();
            }
            if (!string.IsNullOrWhiteSpace(_userinfo.RydNoQueryUserId(inspectRydNo.RYD_RyderNo)))
            {
                if (_userinfo.QueryUserInfo(inspectRydNo.RYD_Id).RYD_RyderNo != inspectRydNo.RYD_RyderNo)
                {
                    return NotFound();
                }
            }
            if (string.IsNullOrWhiteSpace(inspectRydNo.ID))
            {
                return NotFound();
            }
            else if (inspectRydNo.ID.Length != 6)
            {
                return NotFound();
            }
            return Ok(await _userinfo.InspectRydNoAsync(inspectRydNo));
        }

        /// <summary>
        /// 获取微信OpenId，如果不存在，则创建
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("/api/GetWxNo")]
        public async Task<IActionResult> GetWxNo(string code,int type)
        {
            if (string.IsNullOrWhiteSpace(code) || code == "null")
            {
                return NotFound();
            }
            if (type != 4 && type != 0)
            {
                return NotFound();
            }
            string openId = _userinfo.GetOpenId(code);
            if (string.IsNullOrWhiteSpace(openId))
            {
                openId = await _userinfo.OAuth2(code);
                if(string.IsNullOrWhiteSpace(openId))
                {
                    return NotFound();
                }
                else
                {
                    if (_userinfo.UpdateWxCode(code,_userinfo.RegUser(openId, type)))
                    {
                        return Ok(openId);
                    }
                    else
                    {
                        return NoContent();
                    }
                }
            }
            else
            {
                return Ok(openId);
            }

        }
    }
}
